﻿#region Header
// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file=IdManager.cs company="Tekla">
// //   Copyright (c) Tekla 2011-2013
// // </copyright>
// // <general information>
// //   User: Pool Charles; Time:11:29 AM
// // </general information>
// // --------------------------------------------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using DetailManager.ProxyInterfaceItems;

namespace DetailManager.Tools
{
    public static class IdManager
    {
        private static readonly Random Generator;
        private static List<int> _usedIdValues;

        static IdManager()
        {
            _usedIdValues = new List<int>();
            Generator = new Random();
            _usedIdValues.Add(TreePresenterDatabase.UnsortedRootId);
        }

        private static List<int> UsedIdValues
        {
            get { return _usedIdValues; }
            set { _usedIdValues = value; }
        }

        public static void AddValue(int i)
        {
            if (UsedIdValues.Contains(i))
            {
                Debug.WriteLine("Warning: IdManger Add function called for Id: {0}, " +
                                "this was not added because it would cause duplicate value.", i);
            }
            else
                UsedIdValues.Add(i);
        }

        public static int GetNewId()
        {
            int result;
            while (UsedIdValues.Contains(result = Generator.Next()))
            { }
            UsedIdValues.Add(result);
            return result;
        }
    }
}